﻿using System;
using System.Collections.Generic;
using System.Linq;
using Tauren.Business.DLL.Base;
using Tauren.Business.DLL.Resouces;
using Tauren.Business.Entity.Config;
using Tauren.Business.Entity.Const;
using Tauren.Business.Entity.Enum;
using Tauren.Business.Entity.Model;
using Tauren.Business.Entity.Output;
using Tauren.Business.Infrastructure.Globals;

namespace Tauren.Business.Service.DialogService
{
    public class DialogServcie : Base.BaseService, IDialogServcie
    {
        public DialogServcie(IBaseDLL baseDLL, IConnectionBLLBase connectionBLL, IMenuResourcesBusiness resourcesBusiness) : base(baseDLL, connectionBLL, resourcesBusiness)
        {
            this.ResourcesInfo = this.GetResources(GlobalConst.RESOURCES_FLAG_MODULE_COMPANY_KEY);
        }

        /// <summary>
        /// 弹出组织
        /// </summary>
        /// <returns></returns>
        public IResponseMessage DialogOrg()
        {
            List<Entity.Model.Unitinfo> Companys = baseDLL.All<Entity.Model.Unitinfo>();
            List<Entity.Model.Organization> Organizations = baseDLL.All<Entity.Model.Organization>();
            List<GlobalTree> globalTrees = new List<GlobalTree>();
            Companys.Where(o => o.ParentId == 0 && o.Enable && !o.HaveDelete).ToList().ForEach(o =>
            {
                globalTrees.Add(new GlobalTree()
                {
                    Icon = "",
                    Id = o.UnitId,
                    Label = Global.Language == (LanguageType.zh_cn.ToString().Replace("_", "-")) ? o.CNName : string.IsNullOrEmpty(o.ENName) ? o.CNName : o.ENName,
                    Code = o.UnitCode,
                    CNName = o.CNName,
                    ENName = o.ENName,
                    ParentId = o.ParentId.Value,
                    Children = GenerateChildUnitTreeInfo(o.UnitId, Companys, Organizations),
                    Disabled = true,
                    UseNodeCss = !o.Enable || o.HaveDelete,
                    NodeType = OrgationalType.unit.ToString(),
                    Level = o.Level
                });
            });
            return globalTrees.Succeed();
        }

        private List<GlobalTree> GenerateChildUnitTreeInfo(int ParentId, List<Unitinfo> companys, List<Organization> organizations, bool isOrg = true)
        {
            List<GlobalTree> _OutPuts = new List<GlobalTree>();
            List<Unitinfo> childMenus = companys.Where(o => !o.HaveDelete && o.ParentId == ParentId && o.Enable && !o.HaveDelete).ToList();
            if (childMenus.Any())
            {
                childMenus.ForEach(o =>
                {
                    _OutPuts.Add(new GlobalTree()
                    {
                        Icon = "",
                        Id = o.UnitId,
                        Label = Global.Language == (LanguageType.zh_cn.ToString().Replace("_", "-")) ? o.CNName : string.IsNullOrEmpty(o.ENName) ? o.CNName : o.ENName,
                        Code = o.UnitCode,
                        CNName = o.CNName,
                        ENName = o.ENName,
                        ParentId = o.ParentId.Value,
                        Children = GenerateChildUnitTreeInfo(o.UnitId, companys, organizations, isOrg),
                        Disabled = isOrg,
                        UseNodeCss = !o.Enable || o.HaveDelete,
                        NodeType = OrgationalType.unit.ToString(),
                        Level = o.Level
                    });
                });
            }
            List<Organization> orgs = organizations.Where(o => o.ParentId == 0 && o.UnitId == ParentId && o.Enable && !o.HaveDelete).ToList();
            if (orgs.Any())
            {
                orgs.ForEach(o =>
                {
                    _OutPuts.Add(new GlobalTree()
                    {
                        Icon = "",
                        Id = o.OrgId,
                        Label = Global.Language == (LanguageType.zh_cn.ToString().Replace("_", "-")) ? o.CNName : string.IsNullOrEmpty(o.ENName) ? o.CNName : o.ENName,
                        Code = o.OrgCode,
                        CNName = o.CNName,
                        ENName = o.ENName,
                        ParentId = o.ParentId.Value,
                        Children = GenerateChildOrgTreeInfo(o.OrgId, organizations, isOrg),
                        NodeType = OrgationalType.organization.ToString(),
                        Parameters = new List<Options>() { new Entity.Output.Options() { Label = "companyId", Value = o.UnitId } },
                        Level = o.Level
                    });
                });
            }
            return _OutPuts;
        }

        private List<GlobalTree> GenerateChildOrgTreeInfo(int ParentId, List<Organization> organizations, bool isOrg)
        {
            List<GlobalTree> _OutPuts = new List<GlobalTree>();
            List<Organization> orgs = organizations.Where(o => o.ParentId == ParentId && o.Enable && !o.HaveDelete).ToList();
            if (orgs.Any())
            {
                orgs.ForEach(o =>
                {
                    _OutPuts.Add(new GlobalTree()
                    {
                        Icon = "",
                        Id = o.OrgId,
                        Label = Global.Language == (LanguageType.zh_cn.ToString().Replace("_", "-")) ? o.CNName : string.IsNullOrEmpty(o.ENName) ? o.CNName : o.ENName,
                        Code = o.OrgCode,
                        CNName = o.CNName,
                        ENName = o.ENName,
                        ParentId = o.ParentId.Value,
                        Children = GenerateChildOrgTreeInfo(o.OrgId, organizations, isOrg),
                        UseNodeCss = !o.Enable || o.HaveDelete,
                        NodeType = OrgationalType.organization.ToString(),
                        Parameters = new List<Options>() { new Entity.Output.Options() { Label = "companyId", Value = o.UnitId.Value } },
                        Level = o.Level
                    });
                });
            }
            return _OutPuts;
        }

        /// <summary>
        /// 弹出职位选择窗体
        /// </summary>
        /// <returns></returns>
        public IResponseMessage DialogPosition()
        {
            List<Entity.Model.Unitinfo> Companys = baseDLL.All<Entity.Model.Unitinfo>();
            List<Entity.Model.Organization> Organizations = baseDLL.All<Entity.Model.Organization>();
            List<GlobalTree> globalTrees = new List<GlobalTree>();
            Companys.Where(o => o.ParentId == 0 && o.Enable && !o.HaveDelete).ToList().ForEach(o =>
            {
                globalTrees.Add(new GlobalTree()
                {
                    Icon = "",
                    Id = o.UnitId,
                    Label = Global.Language == (LanguageType.zh_cn.ToString().Replace("_", "-")) ? o.CNName : string.IsNullOrEmpty(o.ENName) ? o.CNName : o.ENName,
                    Code = o.UnitCode,
                    CNName = o.CNName,
                    ENName = o.ENName,
                    ParentId = o.ParentId.Value,
                    Children = GenerateChildUnitTreeInfo(o.UnitId, Companys, Organizations, false),
                    NodeType = OrgationalType.unit.ToString()
                });
            });
            return globalTrees.Succeed();
        }
    }
}
